<template>
  <div class="app-container">
    <!-- *************************************搜索区************************************* -->
    <!-- 分类 -->
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" v-show="showSearch" ref="queryForm" size="small" :inline="true">
          <el-row>
            <el-form-item prop="combinationName">
              <el-select
                @change="getList"
                clearable
                style="width: 100%"
                v-model="queryParams.type"
                :placeholder="$t('请选择')"
              >
                <!--  1-头程 2-中程 3-尾程 -->
                <el-option :label="$t('头程')" :value="1" />
                <el-option :label="$t('中程')" :value="2" />
                <el-option :label="$t('尾程')" :value="3" />
              </el-select>
            </el-form-item>
            <el-form-item prop="expressName">
              <el-input
                :placeholder="$t('快递公司名')"
                v-model="queryParams.expressName"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <el-button v-hasPermi="['distribution:expressCompany:add']" type="primary" plain size="mini" @click="handleAdd">
          {{ $t('新增') }}
        </el-button>
      </template>
    </PageSearch>
    <el-table :height="calTableHeight()" v-loading="loading" :data="tableData" ref="tableRef">
      <el-table-column show-overflow-tooltip prop="expressName" :label="$t('快递公司名')" align="center" width="150" />
      <el-table-column show-overflow-tooltip prop="type" :label="$t('类型')" align="center">
        <template #default="{ row }">
          <!--  1-头程 2-中程 3-尾程 -->
          <div v-if="row.type == 1" :style="{ color: $randomColor(row.type) }">{{ $t('头程') }}</div>
          <div v-if="row.type == 2" :style="{ color: $randomColor(row.type) }">{{ $t('中程') }}</div>
          <div v-if="row.type == 3" :style="{ color: $randomColor(row.type) }">{{ $t('尾程') }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="net" :label="$t('查询地址')" align="center">
        <template #default="{ row }">
          <el-link target="_blank" :href="row.net" type="primary">{{ row.net }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="admin" :label="$t('负责人')" align="center" />
      <el-table-column show-overflow-tooltip prop="tel" :label="$t('电话')" align="center" />
      <el-table-column show-overflow-tooltip prop="billingMethod" :label="$t('计费规则')" align="center" />
      <el-table-column show-overflow-tooltip prop="settlementMethod" :label="$t('结算方式')" align="center" />
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)" v-hasPermi="['distribution:expressCompany:edit']">
            {{ $t('编辑') }}
          </el-button>
          <el-button type="text" @click="handleContract(row)" v-hasPermi="['distribution:expressCompany:selectExpressContract']">
            {{ $t('查看合同') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row.id)" v-hasPermi="['distribution:expressCompany:remove']">
            {{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddAndEdit
      :show.sync="addAndEdit.show"
      :formData="addAndEdit.formData"
      :type="addAndEdit.type"
      @getList="getList"
    />
    <Contract :show.sync="contract.show" :formData="contract.formData" @getList="getList" />
  </div>
</template>

<script>
import { expressCompanyList, delExpressCompany } from '@/api/basicSetting/expressCompany'
import AddAndEdit from './components/addAndEdit.vue'
import Contract from './components/contract.vue'

export default {
  activated() {
    this.$nextTick(() => this.$refs['tableRef']?.doLayout())
  },
  components: { AddAndEdit, Contract },
  data() {
    return {
      addAndEdit: {
        show: false,
        formData: {},
      },
      contract: {
        show: false,
        formData: {},
      },
      showSearch: false,
      tableData: [],
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },

  created() {
    this.getList()
  },
  methods: {
    handleContract(row) {
      this.contract.formData = { ...row }
      this.contract.show = true
    },
    handleAdd(row) {
      this.addAndEdit.type = 'add'
      this.addAndEdit.formData = {}
      this.addAndEdit.show = true
    },
    handleEdit(row) {
      this.addAndEdit.type = 'edit'
      this.addAndEdit.formData = { ...row }
      this.addAndEdit.show = true
    },

    handleDelete(id) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        delExpressCompany(id).then((res) => {
          if (res.code == 200) {
            this.$message({ type: 'success', message: res.msg })
            this.getList()
          }
        })
      })
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      expressCompanyList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
        this.$nextTick(() => this.$refs['tableRef']?.doLayout())
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped></style>
